package org.jeecg.modules.cosmetic.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Iterator;
import java.util.Set;

/**
 * @author scx
 * @date 2022/7/8 1:23 下午
 */
@Slf4j
public class MyValidateUtil {

    private static Validator validator;

    static {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    private static String validateObject(Object obj, boolean key, Class<?>... groups) {
        return validateObject(obj, "业务", key, groups);
    }

    /**
     * 验证对象
     */
    private static String validateObject(Object obj, String name, boolean key, Class<?>... groups) {
        if (null == obj) {
            return name + "参数全部为空";
        }
        Set<ConstraintViolation<Object>> set = validator(obj, groups);
        if (set != null && set.size() > 0) {
            Iterator<ConstraintViolation<Object>> iterator = set.iterator();
            StringBuffer sb = new StringBuffer();
            while (iterator.hasNext()) {
                ConstraintViolation<Object> o = iterator.next();
                String messageTemplate = o.getMessageTemplate();

                if (key) {
                    sb.append(o.getPropertyPath()).append(":");
                }

                if (messageTemplate.indexOf("{") >= 0 && messageTemplate.indexOf("}") >= 0) {
                    if (messageTemplate.indexOf("javax.validation.constraints.NotNull") >= 0
                            || messageTemplate.indexOf("javax.validation.constraints.NotEmpty") >= 0
                            || messageTemplate.indexOf("org.hibernate.validator.constraints.NotEmpty") >= 0
                            || messageTemplate.indexOf("javax.validation.constraints.NotBlank") >= 0
                            || messageTemplate.indexOf("org.hibernate.validator.constraints.NotBlank") >= 0
                    ) {
                        sb.append("必填");
                    } else if (messageTemplate.indexOf("org.hibernate.validator.constraints.Length") >= 0) {
                        sb.append("值的长度超长");
                    } else {
                        sb.append("值不正确");
                    }
                } else {
                    sb.append(o.getMessage());
                }
                sb.append(";");
            }
            return sb.toString();
        } else {
            return "";
        }
    }

    private static Set<ConstraintViolation<Object>> validator(Object obj, Class<?>... groups) {
        return validator.validate(obj, groups);
    }

    /**
     * 验证
     *
     * @param obj
     */
    public static void validThrow(Object obj) {
        validThrow(obj, true);
    }

    public static void validThrow(Object obj, boolean key) {
        String errMsg = validateObject(obj, key);
        if (!StringUtils.isBlank(errMsg)) {
            throw new JeecgBootException(errMsg);
        }
    }

    public static void validPutMsg(Object obj, StringBuffer errMsg) {
        String msg = validateObject(obj, true);
        if (!StringUtils.isBlank(msg)) {
            errMsg.append(msg).append(";");
        }
    }
    public static String valid(Object obj) {
        return valid(obj, true);
    }

    public static String valid(Object obj, boolean key) {
        return validateObject(obj, key);
    }

    public static boolean validRtErr(Object obj) {
        String msg = validateObject(obj, true);
        boolean b = !StringUtils.isBlank(msg);
        if (b) {
            log.error("公共验证错误提示：msg：{}", msg);
        }
        return b;
    }
}
